package service

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"math/rand"
	"pq/helper"
	"strconv"
	"time"
)

type CategoryService struct{

}

//func (cs CategoryService) GetItemResrouceTree (data []orm.Params) []helper.Tree
func (cs CategoryService) GetDeepTree(cates []orm.Params,pid int)[]helper.Tree{
	treedata := []helper.Tree{}
	rand.Seed(time.Now().UnixNano())
	min := 10
	rightMax := 300
	max := 1000
	for _,c := range cates{
		id,err := strconv.Atoi(c["id"].(string))
		pid,_ := strconv.Atoi(c["pid"].(string))
		finishRate := fmt.Sprintf("%d/%d",rand.Intn(rightMax-min+1) + min , rand.Intn(max-rightMax+1)+rightMax)
		//just for now
		finishRate = ""
		star := fmt.Sprintf("%d",rand.Intn(5-1+1) + 1)
		if err == nil {
			treedata = append(treedata, helper.Tree{
				Id: id,
				Pid:   pid,
				Title: c["title"].(string),
				Rgt:   c["rgt"].(string),
				Lft:   c["lft"].(string),
				Level: c["level"].(string),
				Code: c["code"].(string),
				FinishRate: finishRate,
				FinishStar: star,
				Image : c["image"].(string),
				Ordering : c["ordering"].(string),
				Children:nil,
			})
		}
	}
	maker := helper.DeepTree{}
	return maker.Make(treedata,pid)
}
